package top.abilities.util.plugin;



import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import top.abilities.util.util.AESUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class CommonPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static Map<String,String> propertyMap;
    private String[] propertyNames = {
            "config.jdkpl.password","config.jdka.password"
    };

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        propertyMap = new HashMap<>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            for (String p : propertyNames) {
                if (p.equalsIgnoreCase(keyStr)) {
                    value= AESUtil.AESDecode(value);
                    break;
                }
            }
            propertyMap.put(keyStr, value);
        }
    }
    //static method for accessing context properties
    public static Object getProperty(String name) {
        return propertyMap.get(name);
    }
}
